package net.springmvc17_18.controller;

import net.springmvc17_18.entity.Stu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/stu")
public class StuController {

    @RequestMapping(value = "/hh", method = RequestMethod.POST)
//    @PostMapping()
//    @ResponseBody
    public String login() {
        System.out.println("login");
        return "hh";
    }

    @GetMapping("data")
    @ResponseBody
    public String data(@RequestParam(value = "name1") String name,
                       @RequestParam(required = false,defaultValue = "333") String sn) {
        System.out.println(name+" "+sn);
        return name+" "+sn;
    }

    // 一名多值

    @GetMapping("data1")
    @ResponseBody
    public String data1(@RequestParam List<String> sns) {
        System.out.println(sns);
        return "hh";
    }

    // 实体接收
    @GetMapping("data2")
    @ResponseBody
    public String data2(Stu stu) {
        System.out.println(stu);
        return "hh";
    }

    // 路径传参
    @GetMapping("data3/{id1}")
    @ResponseBody
    public String data3(@PathVariable(value = "id1") Integer id) {
        System.out.println(id);
        return "hh";
    }

    //@RequestBody
    @PostMapping("data4")
    @ResponseBody
    public String data4(@RequestBody Stu stu) {
        System.out.println(stu);
        return "hh";
    }

    //@CookieValue

    @PostMapping("data5")
    @ResponseBody
    public String data5(@CookieValue("JSESSIONID") String jsessionid,
                        @CookieValue("cookie") String cookie) {
        System.out.println(jsessionid+" "+cookie);
        return "hh";
    }

    //@RequestHeader注解
    @PostMapping("data6")
    @ResponseBody
    public String data6(@RequestHeader(value = "Content-Type") String type,
                        @RequestHeader("Host") String host) {
        System.out.println(type+" "+host);
        return "hh";
    }

    // request 级别共享域
    @GetMapping("data7")
    public String data7(Model model) {
        model.addAttribute("mode", "mode数据");
        return "hh";
    }

    @GetMapping("data8")
    public String data8(ModelMap modelMap) {
        modelMap.addAttribute("mode", "modelMap数据");
        return "hh";
    }

    @GetMapping("data9")
    public String data9(Map<String,Object> map) {
        map.put("mode", "map数据");
        return "hh";
    }
    @GetMapping("data10")
    public String data10(HttpServletRequest request) {
        request.setAttribute("mode", "request数据");
        return "hh";
    }

    @GetMapping("data11")
    public ModelAndView data11(ModelAndView modelAndView) {
        modelAndView.setViewName("hh"); // 视图name
        modelAndView.addObject("mode", "modelAndView数据");
        return modelAndView;
    }

    @GetMapping("data12")
    public String data12(HttpSession httpSession) {
        httpSession.setAttribute("mode","session数据"); // 视图name
        return "hh";
    }

    @Autowired
    private ServletContext servletContext;
    @GetMapping("data13")
    public String data13() {
        servletContext.setAttribute("mode","servletContext数据");
        return "hh";
    }

    // 转发重定向
    @GetMapping("redirect")
    public String redirect() {
//        request.getRequestDispatcher("xx").forward(request,response);
//        response.sendRedirect("");
        return "redirect:/stu/data13";
    }

    // 转发
    @GetMapping("forward")
    public String forward() {
        return "forward:/stu/data13";
    }

}
